unit getIp;

interface

uses
  httpsend;

function GetExternalIp: string;

implementation

uses
  Classes,
  SysUtils;

function GetBody(AHTML: string): string;
const
 BODY_OPEN_TAG  = '<body>';
 BODY_CLOSE_TAG = '</body>';
begin
 result:=Copy(
   AHTML,
   Pos(BODY_OPEN_TAG, LowerCase(AHTML)) + Length(BODY_OPEN_TAG),
   Length(AHTML)
   );
 Delete(
   result,
   Pos(BODY_CLOSE_TAG, result),
   Length(result)
   );
end;

function GetIPAddress(AHTMLBody: string): string;
const
 DELIMITER = ':';
begin
 result:=Trim(Copy(
   AHTMLBody,
   Pos(DELIMITER, AHTMLBody)+1,
   Length(AHTMLBody)
   ));
end;

function RemoveNonIp(AString: string): string;
var
 I: Integer;
begin
 Result := '';
 for I := 1 to Length(AString) do
   if (AString[I] in ['0'..'9','.']) then
     Result := Result + AString[I];
end;

function GetExternalIp: string;
var
  (* usadas para obtener el ip *)
  sl: TStringList;

begin
  Result := '<unknown>';
  try
    sl := TStringList.Create;
    HttpGetText('checkip.dyndns.org', sl);
    sl.Text := RemoveNonIp(GetBody(sl.Text));

    if Length(sl.text) > 0 then
      Result := sl[0];
  finally
    sl.Free;
  end;
end;

end.


